#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
基础 GTest 扩展测试用例
"""

from gtest import test, EXPECT_EQ, EXPECT_TRUE, EXPECT_FALSE
from mc.types import int32_t


@test("BasicMath", "Addition")
def test_addition():
    """测试基本加法运算"""
    a: int32_t = 5
    b: int32_t = 3
    result: int32_t = a + b
    EXPECT_EQ(result, 8)


@test("BasicMath", "Subtraction")
def test_subtraction():
    """测试基本减法运算"""
    a: int32_t = 10
    b: int32_t = 4
    result: int32_t = a - b
    EXPECT_EQ(result, 6)


@test("BasicMath", "Multiplication")
def test_multiplication():
    """测试基本乘法运算"""
    a: int32_t = 7
    b: int32_t = 6
    result: int32_t = a * b
    EXPECT_EQ(result, 42)


@test("BooleanLogic", "TruthValues")
def test_boolean_logic():
    """测试布尔逻辑"""
    is_true: bool = True
    is_false: bool = False

    EXPECT_TRUE(is_true)
    EXPECT_FALSE(is_false)
    EXPECT_TRUE(is_true and not is_false)
    EXPECT_FALSE(is_true and is_false)


@test("StringOperations", "Concatenation")
def test_string_concat():
    """测试字符串连接"""
    hello: str = "Hello"
    world: str = "World"
    result: str = hello + " " + world
    EXPECT_EQ(result, "Hello World")


@test("StringOperations", "Length")
def test_string_length():
    """测试字符串长度"""
    text: str = "MCPy"
    length: int32_t = len(text)
    EXPECT_EQ(length, 4)


@test("MinMaxVariadic", "MinTwoArgs")
def test_min_two_args():
    """测试 min 两个参数"""
    result: int = min(3, 5)
    EXPECT_EQ(result, 3)

    result2: int = min(10, 2)
    EXPECT_EQ(result2, 2)


@test("MinMaxVariadic", "MinThreeArgs")
def test_min_three_args():
    """测试 min 三个参数"""
    result: int = min(5, 2, 8)
    EXPECT_EQ(result, 2)


@test("MinMaxVariadic", "MinMultipleArgs")
def test_min_multiple_args():
    """测试 min 多个参数"""
    result: int = min(5, 2, 8, 1, 9)
    EXPECT_EQ(result, 1)


@test("MinMaxVariadic", "MaxTwoArgs")
def test_max_two_args():
    """测试 max 两个参数"""
    result: int = max(3, 5)
    EXPECT_EQ(result, 5)

    result2: int = max(10, 2)
    EXPECT_EQ(result2, 10)


@test("MinMaxVariadic", "MaxThreeArgs")
def test_max_three_args():
    """测试 max 三个参数"""
    result: int = max(5, 2, 8)
    EXPECT_EQ(result, 8)


@test("MinMaxVariadic", "MaxMultipleArgs")
def test_max_multiple_args():
    """测试 max 多个参数"""
    result: int = max(5, 2, 8, 1, 9)
    EXPECT_EQ(result, 9)


@test("MinMaxVariadic", "MinMaxFloat")
def test_min_max_float():
    """测试 min/max 浮点数"""
    min_result: float = min(1.5, 2.5, 0.5)
    EXPECT_EQ(min_result, 0.5)

    max_result: float = max(1.5, 2.5, 0.5)
    EXPECT_EQ(max_result, 2.5)


@test("MinMaxVariadic", "MinMaxNegative")
def test_min_max_negative():
    """测试 min/max 负数"""
    min_result: int = min(-5, -10, -3, -1)
    EXPECT_EQ(min_result, -10)

    max_result: int = max(-5, -10, -3, -1)
    EXPECT_EQ(max_result, -1)


@test("MathFunctions", "AbsFunction")
def test_abs_function():
    """测试 abs() 函数"""
    # 整数
    result1: int = abs(-42)
    EXPECT_EQ(result1, 42)

    result2: int = abs(10)
    EXPECT_EQ(result2, 10)

    # 浮点数
    result3: float = abs(-3.14)
    EXPECT_EQ(result3, 3.14)


@test("MathFunctions", "PowFunction")
def test_pow_function():
    """测试 pow() 函数"""
    # 整数幂
    result1: int = pow(2, 3)
    EXPECT_EQ(result1, 8)

    result2: int = pow(5, 2)
    EXPECT_EQ(result2, 25)

    # 浮点数幂
    result3: float = pow(2.0, 3.0)
    EXPECT_EQ(result3, 8.0)


@test("ContainerFunctions", "LenFunction")
def test_len_function():
    """测试 len() 函数"""
    # 列表
    numbers: list[int] = [1, 2, 3, 4, 5]
    EXPECT_EQ(len(numbers), 5)

    # 空列表
    empty: list[int] = []
    EXPECT_EQ(len(empty), 0)

    # 字符串
    text: str = "Hello"
    EXPECT_EQ(len(text), 5)


@test("SequenceFunctions", "SumFunction")
def test_sum_function():
    """测试 sum() 函数"""
    numbers: list[int] = [1, 2, 3, 4, 5]
    total: int = sum(numbers)
    EXPECT_EQ(total, 15)

    # 空列表
    empty: list[int] = []
    EXPECT_EQ(sum(empty), 0)


@test("SequenceFunctions", "MinMaxList")
def test_min_max_list():
    """测试 min/max 列表版本"""
    numbers: list[int] = [5, 2, 8, 1, 9]

    min_val: int = min(numbers)
    EXPECT_EQ(min_val, 1)

    max_val: int = max(numbers)
    EXPECT_EQ(max_val, 9)


@test("SequenceFunctions", "SortedFunction")
def test_sorted_function():
    """测试 sorted() 函数"""
    numbers: list[int] = [3, 1, 4, 1, 5, 9, 2, 6]
    sorted_nums: list[int] = sorted(numbers)

    EXPECT_EQ(sorted_nums[0], 1)
    EXPECT_EQ(sorted_nums[7], 9)

    # 原列表不变
    EXPECT_EQ(numbers[0], 3)


@test("SequenceFunctions", "SortedReverse")
def test_sorted_reverse():
    """测试 sorted() 反向排序"""
    numbers: list[int] = [3, 1, 4, 1, 5]
    sorted_nums: list[int] = sorted(numbers, reverse=True)

    EXPECT_EQ(sorted_nums[0], 5)
    EXPECT_EQ(sorted_nums[4], 1)


@test("SequenceFunctions", "RangeLoop")
def test_range_loop():
    """测试 range() 在循环中使用"""
    total: int = 0
    for i in range(5):
        total = total + i

    total1 = 0
    l = [i for i in range(5)]
    for i in l:
        total1 += i

    # 0 + 1 + 2 + 3 + 4 = 10
    EXPECT_EQ(total, 10)
    EXPECT_EQ(total, total1)


@test("SequenceFunctions", "EnumerateFunction")
def test_enumerate_function():
    """测试 enumerate() 函数"""
    items: list[str] = ["a", "b", "c"]
    count: int = 0

    for idx, item in enumerate(items):
        count = count + 1

    EXPECT_EQ(count, 3)


@test("SequenceFunctions", "EnumerateWithStart")
def test_enumerate_with_start():
    """测试 enumerate() 带 start 参数"""
    items: list[int] = [10, 20, 30]
    sum_idx: int = 0

    for idx, item in enumerate(items, start=5):
        sum_idx = sum_idx + idx

    # 5 + 6 + 7 = 18
    EXPECT_EQ(sum_idx, 18)


@test("SequenceFunctions", "ZipFunction")
def test_zip_function():
    """测试 zip() 函数"""
    numbers: list[int] = [1, 2, 3]
    letters_idx: list[int] = [10, 20, 30]
    total: int = 0

    for num, idx in zip(numbers, letters_idx):
        total = total + num + idx

    # (1+10) + (2+20) + (3+30) = 66
    EXPECT_EQ(total, 66)


@test("SequenceFunctions", "ReversedFunction")
def test_reversed_function():
    """测试 reversed() 函数"""
    numbers: list[int] = [1, 2, 3, 4, 5]
    result: list[int] = []

    for num in reversed(numbers):
        result.append(num)

    EXPECT_EQ(result[0], 5)
    EXPECT_EQ(result[4], 1)


@test("BooleanFunctions", "AnyFunction")
def test_any_function():
    """测试 any() 函数"""
    # 包含 True
    flags1: list[bool] = [False, False, True]
    EXPECT_TRUE(any(flags1))

    # 全 False
    flags2: list[bool] = [False, False, False]
    EXPECT_FALSE(any(flags2))


@test("BooleanFunctions", "AllFunction")
def test_all_function():
    """测试 all() 函数"""
    # 全 True
    flags1: list[bool] = [True, True, True]
    EXPECT_TRUE(all(flags1))

    # 包含 False
    flags2: list[bool] = [True, False, True]
    EXPECT_FALSE(all(flags2))


@test("CombinedFunctions", "NestedCalls")
def test_nested_function_calls():
    """测试嵌套的全局函数调用"""
    numbers: list[int] = [-5, -10, -3, -1]

    # abs(min(numbers))
    result: int = abs(min(numbers))
    EXPECT_EQ(result, 10)


@test("CombinedFunctions", "ComplexExpression")
def test_complex_expression():
    """测试复杂的全局函数表达式"""
    numbers: list[int] = [1, 2, 3, 4, 5]

    # sum + len + max
    total: int = sum(numbers)
    count: int = len(numbers)
    maximum: int = max(numbers)

    result: int = total + count + maximum
    # 15 + 5 + 5 = 25
    EXPECT_EQ(result, 25)


@test("Iterator", "ListIterNextBasic")
def test_list_iter_next_basic():
    """测试 iter() 和 next() 的基本功能"""

    total = 0
    numbers = [1, 2, 3, 4, 5]
    __it = iter(numbers)
    while True:
        try:
            num = next(__it)
            total = total + num
        except StopIteration:
            break

    # 1 + 2 + 3 + 4 + 5 = 15
    EXPECT_EQ(total, 15)


@test("Iterator", "ListIterNextUseDefault")
def test_list_iter_next_use_default():
    """测试 iter() 和 next() 的基本功能 - 使用默认值

    测试类型收窄系统：
    - num: Optional[int] = next(__it, None)
    - if not num: 检查后，num 应该收窄为 int
    - 自动插入 .value() 解包
    """

    total = 0
    numbers = [1, 2, 3, 4, 5]
    __it = iter(numbers)
    while True:
        num = next(__it, None)
        if not num:
            break
        # 类型收窄：num 从 Optional[int] 收窄为 int
        # 代码生成会自动插入 num.value()
        total = total + num

    # 1 + 2 + 3 + 4 + 5 = 15
    EXPECT_EQ(total, 15)


@test("Iterator", "DictIterNextBasic")
def test_dict_iter_next_basic():
    """测试 iter() 和 next() 的基本功能"""

    total = 0
    d = {1: 10, 2: 20, 3: 30}
    __it = iter(d)
    while True:
        key = next(__it, None)
        if not key:
            break
        total = total + d[key]
    EXPECT_EQ(total, 60)

    __it1 = iter(d)
    total1 = 0
    while True:
        try:
            key1 = next(__it1)
            total1 = total1 + d[key1]
        except StopIteration:
            break
    EXPECT_EQ(total1, total)


@test("Iterator", "NestedIterator")
def test_nested_iterator():
    """测试 iter() 和 next() 的基本功能"""

    total = 0
    len_count = 0
    matrix = [[1, 2], [3, 4], [5, 6]]
    __it = iter(matrix)
    while True:
        row = next(__it, None)
        if not row:
            break

        len_count += len(row)
        __it1 = iter(row)
        while True:
            num = next(__it1, None)
            if not num:
                break
            total += num

    EXPECT_EQ(total, 21)
    EXPECT_EQ(len_count, 6)
